const WebSocket = require('ws');

((ws) => {
  const server = new ws.Server({ port: 16666 });

  function handleOpen() {
    console.log('open');
  }
  function handleClose() {
    console.log('close');
  }
  function handleError() {
    console.log('err');
  }
  function handleMessage(msg) {
    server.clients.forEach((c) => {
      c.send(msg);
    });
  }
  function handleConnection(s) {
    console.log('WebSocket Connection');
    s.on('message', handleMessage);
  }

  function bindEvent() {
    server.on('open', handleOpen);
    server.on('close', handleClose);
    server.on('error', handleError);
    server.on('connection', handleConnection);
  }

  const init = () => {
    bindEvent();
  };
  init();
})(WebSocket);
